package com.nexgo.payment.fragment.main;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.CardMode;
import com.nexgo.payment.constrant.SettleOpType;
import com.nexgo.payment.fragment.SubMenuDialogFragment;
import com.nexgo.payment.trans.balance.BalanceActivity;
import com.nexgo.payment.trans.detail.TransDetailActivity;
import com.nexgo.payment.trans.detail.TransListActivity;
import com.nexgo.payment.trans.ecbalance.EcBalanceActivity;
import com.nexgo.payment.trans.eclog.EcLogActivity;
import com.nexgo.payment.trans.ecsale.EcSaleActivity;
import com.nexgo.payment.trans.lock.LockTerminalActivity;
import com.nexgo.payment.trans.login.LoginActivity;
import com.nexgo.payment.trans.operator.list.OperatorListActivity;
import com.nexgo.payment.trans.preauth.PreAuthActivity;
import com.nexgo.payment.trans.preauthcom.PreAuthComActivity;
import com.nexgo.payment.trans.preauthcomvoid.PreAuthComVoidActivity;
import com.nexgo.payment.trans.preauthvoid.PreAuthVoidActivity;
import com.nexgo.payment.trans.refund.RefundActivity;
import com.nexgo.payment.trans.sale.SaleActivity;
import com.nexgo.payment.trans.salevoid.SaleVoidActivity;
import com.nexgo.payment.trans.scanpreauthcom.ScanPreAuthComActivity;
import com.nexgo.payment.trans.scanpreauthvoid.ScanPreAuthVoidActivity;
import com.nexgo.payment.trans.scanquery.ScanQueryActivity;
import com.nexgo.payment.trans.scanrefund.ScanRefundActivity;
import com.nexgo.payment.trans.scansale.ScanSaleActivity;
import com.nexgo.payment.trans.scanvoid.ScanVoidActivity;
import com.nexgo.payment.trans.settle.SettleActivity;
import com.nexgo.payment.trans.version.VersionActivity;
import com.nexgo.payment.ui.BaseFragment;
import com.nexgo.payment.ui.main.MainActivity;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.DragLayout;
import com.nexgo.payment.widget.TitleBuilder;
import com.nexgo.payment.widget.gridpage.PageGridLayout;
import com.stx.xhb.xbanner.XBanner;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 主界面
 * Created by Kol(Fang Qiang) on 2017/7/30.
 * usage:
 * modify by 谭忠扬-YuriTam
 */
public class MainFragment extends BaseFragment implements XBanner.XBannerAdapter, PageGridLayout.OnItemListener {
    private static final String TAG = MainFragment.class.getSimpleName();

/*    @BindView(R.id.banner)
    XXBanner mBanner;*/
    @BindView(R.id.tab_page)
    PageGridLayout mPageGridLayout;
    Unbinder unbinder;
    DragLayout mDragLayout;
//    private List<Integer> mImageList;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_main_main, container, false);
        unbinder = ButterKnife.bind(this, view);
        mDragLayout=(DragLayout)getActivity().findViewById(R.id.draglayout);
        initTitle(view);

        return view;
    }

    //初始化标题
    private void initTitle(View view) {
        new TitleBuilder(view)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.slide_bar_icon)
                .setTitleImage(R.drawable.side_logo)
                .build();
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        //初始化幻灯片
//        mImageList = new ArrayList<>();
//        mImageList.add(R.drawable.banner_1);
//        mBanner.setData(mImageList, null);
//        mBanner.setmAdapter(this);

        //初始化功能布局
        mPageGridLayout.setGridFormat(4, 3)
                //TAG PAGE 1
                .addSource(R.string.trans_sale, R.drawable.swipe_card_sale_icon)
                .addSource(R.string.trans_scan_pay,R.drawable.swipe_card_sale_icon)
                .addSource(R.string.trans_refund, R.drawable.refund_icon)
                .addSource(R.string.trans_sale_void, R.drawable.revoke_icon)
                .addSource(R.string.trans_detail, R.drawable.trade_detail_icon)
                .addSource(R.string.trans_login, R.drawable.sign_icon)
                .addSource(R.string.trans_auth, R.drawable.pre_auth_icon)
                //TAG PAGE 2
                .addSource(R.string.trans_sale_insert_card, R.drawable.insert_card_sale_icon)
                .addSource(R.string.trans_ec_sale, R.drawable.ec_cash_icon)
                .addSource(R.string.trans_balance_query, R.drawable.balance_icon)
                .addSource(R.string.manage, R.drawable.manage_icon)
                .addSource(R.string.trans_qpass, R.drawable.qpass_icon)
                //监听回调
                .setListener(this)
                .build();
    }

    @Override
    public void onResume() {
        super.onResume();
//        mBanner.startAutoPlay();
    }

    @Override
    public void loadBanner(XBanner banner, Object model, View view, int position) {
//        ((ImageView) view).setImageResource(mImageList.get(position));
    }
    @OnClick(R.id.title_iv_left)
//    @OnClick({R.id.title_iv_left, R.id.iv_sale, R.id.tv_sale, R.id.iv_scan_pay, R.id.tv_scan_pay})
    public void onClick(View view) {
        if (UIUtils.isDoubleClick()) return;
        switch (view.getId()) {
            case R.id.title_iv_left:
                ((MainActivity) getActivity()).open();
                break;
/*            case R.id.iv_sale://消费
            case R.id.tv_sale:
                startActivity(SaleActivity.newInstance(mContext));
                break;
            case R.id.iv_scan_pay://扫码支付
            case R.id.tv_scan_pay:
                intent2Activity(ScanSaleActivity.class);
                break;*/
            default:
                break;
        }
    }

    @Override
    public void onItemSelect(int resId) {
        // 如果此时左边菜单是开启的状态，则先恢复
        if (mDragLayout.getStatus() == DragLayout.Status.Open) {
            mDragLayout.close();
            return;
        }
        if (UIUtils.isDoubleClick()) return;
        switch (resId) {
            case R.string.trans_sale:
                startActivity(SaleActivity.newInstance(mContext));
                break;
            case R.string.trans_refund:
                doRefund();
//                intent2Activity(RefundActivity.class);
                break;
            case R.string.trans_sale_void:
//              doSaleVoid();
//                wzp add
                intent2Activity(SaleVoidActivity.class);
                break;
            case R.string.trans_detail:
                intent2Activity(TransListActivity.class);
                break;
            case R.string.trans_login:
                intent2Activity(LoginActivity.class);
                break;
            case R.string.trans_auth:
                doPreAuthTrans();
                break;
            case R.string.trans_sale_insert_card:
                startActivity(SaleActivity.newInstance(mContext, CardMode.INSERT));
                break;
            case R.string.trans_ec_sale:
                doEcCashTrans();
                break;
            case R.string.trans_balance_query:
                startActivity(BalanceActivity.newInstance(mContext));
                break;
            case R.string.manage:
                doManageTrans();
                break;
            case R.string.trans_qpass:
                doQpassTrans();
                break;
//            case R.string.trans_print:
//                doPrint();
//                break;
//            wzp add
            case R.string.trans_scan_pay:  //被扫
                doScan();
                break;
        }
    }

    @Override
    public void onPageChange(int pageIndex) {

    }

    /**
     * 扫码
     * 支付渠道选择
     */
    private  void doScanChannel(){
        SubMenuDialogFragment dialogFragment = new SubMenuDialogFragment();
        dialogFragment.addSubMenu(R.string.channel_wechat, R.drawable.wechatpay_icon);
        dialogFragment.addSubMenu(R.string.channel_alipay, R.drawable.alipay_icon);
        dialogFragment.addSubMenu(R.string.channel_unionpay, R.drawable.unionpay_icon);
        dialogFragment.setDialogListener(resId -> {
            if (UIUtils.isDoubleClick()) return;
            switch (resId) {
                case R.string.channel_wechat:
                    startActivity(ScanSaleActivity.newInstance(mContext,true,"01"));
                    break;
                case R.string.channel_alipay:
                    startActivity(ScanSaleActivity.newInstance(mContext,true,"02"));
                    break;
                case R.string.channel_unionpay:
                    startActivity(ScanSaleActivity.newInstance(mContext,true,"03"));
                    break;
                default:
                    break;
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }
    private void doScan() {
        SubMenuDialogFragment dialogFragment = new SubMenuDialogFragment();
        dialogFragment.addSubMenu(R.string.trans_scan_pay,R.drawable.auth_com_icon);
        dialogFragment.addSubMenu(R.string.trans_scan_order,R.drawable.auth_com_icon);
        dialogFragment.addSubMenu(R.string.trans_refund_scan,R.drawable.auth_com_icon);
        dialogFragment.addSubMenu(R.string.trans_scan_query,R.drawable.auth_com_icon);

        dialogFragment.setDialogListener(resId -> {
            if (UIUtils.isDoubleClick()) return;
            switch (resId) {
                case R.string.trans_scan_pay:
                    startActivity(ScanSaleActivity.newInstance(mContext));
                    break;
                case R.string.trans_scan_order:
                    doScanChannel();
                    break;
                case R.string.trans_refund_scan:
                    startActivity(ScanRefundActivity.newInstance(mContext));
                    break;
                case R.string.trans_scan_query:
                    startActivity(ScanQueryActivity.newInstance(mContext));
                default:
                    break;
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }
    /**
     * 退货
     */
    private void doRefund() {
        SubMenuDialogFragment dialogFragment = new SubMenuDialogFragment();
        dialogFragment.addSubMenu(R.string.trans_refund_normal, R.drawable.trans_refund_icon);
        dialogFragment.addSubMenu(R.string.trans_online_refund, R.drawable.online_refund_icon);
        dialogFragment.setDialogListener(resId -> {
            if (UIUtils.isDoubleClick()) return;
            switch (resId) {
                case R.string.trans_refund_normal:
                    startActivity(RefundActivity.newInstance(mContext,false));
                    break;
                case R.string.trans_online_refund:
                    startActivity(RefundActivity.newInstance(mContext,true));
//                    intent2Activity(ScanRefundActivity.class);
                    break;
                default:
                    break;
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }

    /**
     * 撤销
     */
    private void doSaleVoid() {
        SubMenuDialogFragment dialogFragment = new SubMenuDialogFragment();
        dialogFragment.addSubMenu(R.string.trans_sale_void_, R.drawable.trans_void_icon);
        dialogFragment.addSubMenu(R.string.trans_scan_sale_void, R.drawable.scan_refund_void_icon);
        dialogFragment.setDialogListener(resId -> {
            if (UIUtils.isDoubleClick()) return;
            switch (resId) {
                case R.string.trans_sale_void_:
                    intent2Activity(SaleVoidActivity.class);
                    break;
                case R.string.trans_scan_sale_void:
                    intent2Activity(ScanVoidActivity.class);
                    break;
                default:
                    break;
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }

    /**
     * 预授权相关交易
     */
    private void doPreAuthTrans() {
        SubMenuDialogFragment dialogFragment = new SubMenuDialogFragment();
        dialogFragment.addSubMenu(R.string.trans_auth, R.drawable.auth_icon);
        dialogFragment.addSubMenu(R.string.trans_auth_complete_void, R.drawable.auth_com_void_icon);
        dialogFragment.addSubMenu(R.string.trans_auth_complete, R.drawable.auth_com_icon);
//        dialogFragment.addSubMenu(R.string.trans_auth_complete_notify, R.drawable.auth_com_notifiy_icon);
        dialogFragment.addSubMenu(R.string.trans_auth_void, R.drawable.auth_void_icon);
        dialogFragment.addSubMenu(R.string.trans_scan_auth_complete_notify, R.drawable.auth_com_icon);
        dialogFragment.addSubMenu(R.string.trans_scan_auth_void, R.drawable.auth_void_icon);

        dialogFragment.setDialogListener(resId -> {
            if (UIUtils.isDoubleClick()) return;
            switch (resId) {
                case R.string.trans_auth:
                    startActivity(PreAuthActivity.newInstance(mContext, null));
                    break;
                case R.string.trans_auth_complete:
                    intent2Activity(PreAuthComActivity.class);
                    break;
  /*              case R.string.trans_auth_complete_notify:
                    intent2Activity(PreAuthComNotifyActivity.class);
                    break;*/
                case R.string.trans_scan_auth_complete_notify:
                    intent2Activity(ScanPreAuthComActivity.class);
                    break;
                case R.string.trans_scan_auth_void:
                    intent2Activity(ScanPreAuthVoidActivity.class);
                    break;
                case R.string.trans_auth_void:
                    intent2Activity(PreAuthVoidActivity.class);
                    break;
                case R.string.trans_auth_complete_void:
                    intent2Activity(PreAuthComVoidActivity.class);
                    break;

                default:
                    break;
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }

    /**
     * 电子现金相关交易
     */
    private void doEcCashTrans() {
        SubMenuDialogFragment dialogFragment = new SubMenuDialogFragment();
        dialogFragment.addSubMenu(R.string.trans_qpboc, R.drawable.tap_card_icon);
        dialogFragment.addSubMenu(R.string.trans_ec_normal_sale, R.drawable.insert_card_icon);
        dialogFragment.addSubMenu(R.string.trans_ec_log_query, R.drawable.query_list_icon);
        dialogFragment.addSubMenu(R.string.trans_ec_balance_query, R.drawable.query_balance_icon);
        dialogFragment.setDialogListener(resId -> {
            if (UIUtils.isDoubleClick()) return;
            switch (resId) {
                case R.string.trans_qpboc:
                    startActivity(EcSaleActivity.newInstance(mContext, CardMode.TAP));
                    break;
                case R.string.trans_ec_normal_sale:
                    startActivity(EcSaleActivity.newInstance(mContext, CardMode.INSERT));
                    break;
                case R.string.trans_ec_log_query:
                    intent2Activity(EcLogActivity.class);
                    break;
                case R.string.trans_ec_balance_query:
                    intent2Activity(EcBalanceActivity.class);
                    break;
                default:
                    break;
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }

    /**
     * 管理相交操作
     */
    private void doManageTrans() {
        SubMenuDialogFragment dialogFragment = new SubMenuDialogFragment();
        dialogFragment.addSubMenu(R.string.trans_print, R.drawable.print_order_icon);
        dialogFragment.addSubMenu(R.string.trans_settle, R.drawable.trans_settle_icon);
        dialogFragment.addSubMenu(R.string.operator_info, R.drawable.operator_icon);
        dialogFragment.addSubMenu(R.string.lock_terminal, R.drawable.lock_ter_icon);
        dialogFragment.addSubMenu(R.string.version_info, R.drawable.version_icon);
        dialogFragment.setDialogListener(resId -> {
            if (UIUtils.isDoubleClick()) return;
            switch (resId) {
                case R.string.trans_print:
                    doPrint();
                    break;
                case R.string.trans_settle:
                    startActivity(SettleActivity.newInstance(mContext, SettleOpType.NORMAL_SETTLE));
                    break;
                case R.string.operator_info:
                    intent2Activity(OperatorListActivity.class);
                    break;
                case R.string.lock_terminal:
                    intent2Activity(LockTerminalActivity.class);
                    break;
                case R.string.version_info:
                    intent2Activity(VersionActivity.class);
                    break;
                default:
                    break;
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }

    /**
     * 闪付凭密
     */
    private void doQpassTrans() {
        SubMenuDialogFragment dialogFragment = new SubMenuDialogFragment();
        dialogFragment.addSubMenu(R.string.trans_sale_qpass, R.drawable.password_blue_icon);
        dialogFragment.addSubMenu(R.string.trans_pre_auth_qpass, R.drawable.password_yellow_icon);
        dialogFragment.setDialogListener(resId -> {
            if (UIUtils.isDoubleClick()) return;
            switch (resId) {
                case R.string.trans_sale_qpass:
                    startActivity(SaleActivity.newInstance(mContext, CardMode.TAP, true));
                    break;
                case R.string.trans_pre_auth_qpass:
                    startActivity(PreAuthActivity.newInstance(mContext, CardMode.TAP, true));
                    break;
                default:
                    break;
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }

    /**
     * 打印相关
     */
    private void doPrint() {
        SubMenuDialogFragment dialogFragment = new SubMenuDialogFragment();
        dialogFragment.addSubMenu(R.string.reprint_last_trans, R.drawable.print_order_icon);
        dialogFragment.addSubMenu(R.string.print_trans_gather, R.drawable.trans_sum_icon);
        dialogFragment.addSubMenu(R.string.reprint_trans_settle, R.drawable.trans_settle_icon);
        dialogFragment.setDialogListener(resId -> {
            if (UIUtils.isDoubleClick()) return;
            switch (resId) {
                case R.string.reprint_last_trans:
                    startActivity(TransDetailActivity.newInstance(mContext, null));
                    break;
                case R.string.print_trans_gather:
                    startActivity(SettleActivity.newInstance(mContext, SettleOpType.PRINT_SUM_INFO));
                    break;
                case R.string.reprint_trans_settle:
                    startActivity(SettleActivity.newInstance(mContext, SettleOpType.REPRINT_SETTLE_INFO));
                    break;
                default:
                    break;
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }

    @Override
    public void onStop() {
        super.onStop();
//        mBanner.stopAutoPlay();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
